********************************************************************************

********************************************************************************
****							Merge with Mig+								****
********************************************************************************

use "\suf_personen.dta"
append using "\personen.dta"

********************************************************************************
****						Generating weights								****
********************************************************************************

ssc install ebalance // uses a preprocessing scheme where covariate balance 
					// is directly built into the weight function that is used 
					// to adjust the control units.
ssc install fre // ado for better tabulation
					
					
sort HHLFD INTNR

* Generate var for education like Mikrozensus
					
recode h11014 (1 = 1) (-95 2 3 = 2) (4/6 = 3) (7 8 = 4) (9 10 = .), gen(mz_schul)
		label define mz_schul 1 "In education" 2 "No education/ lower secondary edu." ///
		3 "Secondary edu." 4 "High school diploma", modify
		label value mz_schul mz_schul
		tab mz_schul					
					
* Generate var for generation status

recode k_Migra2Gen (4 = 1) (1 = 2) (2 3 1002 = 3), gen(generation) 
	lab var generation "Generation status" // only up to 2. Gen.
	lab define generation 1 "native" 2 "1. Generation" 3 "2. Generation" 
	lab value generation generation
	tab generation
	
* Generate var gender (excluding diverse)
gen sex_balance = GESCHLECHT
replace sex_balance = . if sex_balance==3


* Generate weight: Interaction of gender and generation status, education separately

	// base weight (provided by AID:A) is included
	
		ebalance sex_balance#generation i.mz_schul, manualtargets(0.0846 0.0477 0.378 0.0826 0.0443 0.282 0.259 0.289) gen(mz_w2) basewt(z_gew)		
		svyset [pweight=mz_w2]
		tab GESCHLECHT generation, cell
		svy: tab GESCHLECHT generation


********************************************************************************
****						Recode der Vars									****
********************************************************************************


********************************************************************************
*** Tag each household (Mother of child unter 18 years old that lives in household)
sort HHLFD INTNR
egen hhid = tag(HHLFD) if k_IstETvonZP17==1 & k_extern==0 & GESCHLECHT==2

********************************************************************************

*** Delete Cases of individuals who do not live in household
drop if k_extern==1

********************************************************************************


*** Immigrant status
	
	// Generation status
	label var generation "Generation status"
	
	// Citizenship (mothers)
	recode k_natio (1 = 1) (2 = 2) (3 4 6 = 3) (5 = .), gen(land_grup)
		label var land_grup "Citizenship"
		label define land_grup 1 "German" 2 "EU citizen" 3 "Non-EU citizen"
		label value land_grup land_grup
		tab land_grup
		
	// Immigrant Status Mothers (Generation + Citizenship)
	gen gen_sta = .
		replace gen_sta = 0 if generation==1
		replace gen_sta = 1 if generation==2 & land_grup==1 
		replace gen_sta = 2 if generation==2 & land_grup==2 
		replace gen_sta = 3 if generation==2 & land_grup==3
		replace gen_sta = 4 if generation==3 
	lab var gen_sta "Immigrant Status"
	lab define gen_sta 0 "Non-immigrant" 1 "1. Gen. - German" 2 "1. Gen. - EU" ///
		3 "1. Gen. - non-EU" 4 "2. Gen."
	lab value gen_sta gen_sta
	ta gen_sta
	

*** Family type
	// For all
	
	recode k_familientyp_hh (6 7 = 0) (4 5 = 1) (1/3 8 9/16 = .), gen(nuc_sing)
	tab nuc_sing 
	label var nuc_sing "Family type"
	label define nuc_sing 0 "Two-parent Family" 1 "Single-Parent-Family"
	label value nuc_sing nuc_sing
	tab nuc_sing if k_IstETvonZP17==1 // 8.002 nuclear, 741 single-parent
	
	// Just mothers
	gen nuc_sing_mum = nuc_sing if k_IstETvonZP17==1 & GESCHLECHT==2
	replace nuc_sing_mum = . if nuc_sing_mum==1 & k_IstETvonZP17==1 & GESCHLECHT==1
	tab nuc_sing_mum
			
	egen family_mum = max(nuc_sing_mum), by(HHLFD)
	label define single_moms 0 "Two-parent families" 1 "Single-Mothers", modify
	label value family_mum single_moms
	lab var family_mum "Family type"
	tab family_mum
	
	
*** Poverty Risk
		// 0 = income above 60% (approx. 20.000€/year), 1= below
		
		recode k_arm60p_hh (0 = 0) (1 = 1), gen(poverty_risk)
		label define poverty_risk 0 "above poverty threshold" 1 "below poverty threshold"
		label value poverty_risk poverty_risk
		label var poverty_risk "Poverty Risk"
		replace poverty_risk=0 if poverty_risk<0.5 
		replace poverty_risk=1 if poverty_risk>=0.5 
		tab poverty_risk 		
		
*** Life Satisfaction
	tab h77100
	egen life_satis = max(h77100) if k_IstETvonZP17==1 & k_extern==0 & GESCHLECHT==2, by(HHLFD)
	lab var life_satis "Life satisfaction (mother)"
	tab life_satis
	
*** Psychosocial difficulties children (see other do-file)
	// Imputed data is needed
	
*** CONTROL VARIABLES

	// Region
	recode bland (1/10=0) (11/16=1), gen(region)
	label var region "Region"
	label define region 0 "West Germany" 1 "East Germany", modify
	label value region region
	tab region
	
	// Education (Mother)
	recode k_casmin (0/2=0) (3 4 = 1) (5 6 = 2) (7 8 = 3) if k_IstETvonZP17==1 & GESCHLECHT==2, gen(casmin_mu)
	label define casmin_neu 0 "no degree" 1 "some secondary education with or without vocational training" ///
					2 "college degree with or without vocational training" ///
					3 "university degree", modify
			label value casmin_mu casmin_neu
			label var casmin_mu "Educational attainment (mother)"
			tab casmin_mu
	
		// merge to all members of the household
		egen casmin_mum = max(casmin_mu), by(HHLFD)
		ta casmin_mum	
		label value casmin_mum casmin_neu
		label var casmin_mum "Educational attainment (mother)"
		ta casmin_mum if hhid==1
	

	// Age (mother)
	egen age_mum=max(XALTER) if k_IstETvonZP17==1 & GESCHLECHT==2, by(HHLFD)
	label var age_mum "Age of mother"
	
	// Age youngest child
	egen alt_jk= min(XALTER), by(HHLFD)
	sum alt_jk if XALTER<=17 // 5,8 J.
	label var alt_jk "Age of youngest child"
	replace alt_jk =. if alt_jk>=18
	tab alt_jk
	
	// Number of children
	recode k_AnzKidsHH (1 =1) (2 = 2) (3/10 = 3), gen(kids_hh)
	label define kids_hh 1 "1 child" 2 "2 children" 3 "3 or more children", modify
	label value kids_hh kids_hh
	tab kids_hh
	
		// merge to household
		egen nr_kids_hh = max(kids_hh), by(HHLFD)
		replace nr_kids_hh=. if nr_kids_hh==0
		label value nr_kids_hh kids_hh
		label var nr_kids_hh "Number of children in the household"
		tab nr_kids_hh
		
	// Depressiveness
		tab v44401_1 
		tab v44401_2
		tab v44401_3
		tab v44401_4
		tab v44401_5
		
	egen depri = rowmean(v44401_1 v44401_2 v44401_3 v44401_4 v44401_5)
	replace depri = 1 if depri >=1 & depri<3
	replace depri = 2 if depri >=3 & depri<5
	replace depri = 3 if depri >=5 & depri<=6
		
	label define depri 1 "good mood" 2 "okay" 3 "bad mood"
	label value depri depri
	label var depri "Depressiveness"
	tab depri
		
	egen max_depri_mum=max(depri) if k_IstETvonZP17==1 & GESCHLECHT==2 & k_extern==0, by(HHLFD)
	egen depri_mum=max(max_depri_mum), by(HHLFD)
	label value depri_mum depri
	label var depri_mum "Depressiveness of mother"
	
	// Working situation (mother)
	fre h71319
	recode h71319 (1 3 6 7 9 11 = .) (2 = 1) (4 5 =2) (.t = 3) if k_IstETvonZP17 ///
			& GESCHLECHT==2, gen(work_sit_mu)
	label define work_sit_mu 1 "Full time" 2 "Part time" 3 "Not working"
	label value work_sit_mu work_sit_mu
	label var work_sit_mu "Working situation (mother)"
	fre work_sit_mu
	
		// merge to all household members
		egen working_mum = max(work_sit_mu), by(HHLFD)
		label value working_mum work_sit_mu
		label var working_mum "Working situation (mother)"
		
		
		
	// Net equivalent income
	lab var k_aequi_mean_hh "Equivalent income (Mean interval)"
	gen aequi_1000 = k_aequi_mean_hh/1000
	
********************************************************************************
	
save "\aida_mig.dta", replace
